<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <title>component vue</title>
    <script src="js/vue.js"></script>
</head>
<body>
<div id="app">
    <my-component></my-component>
    <child msg="hello vue"></child>
    <div>
        自定义事件
        <template id="child-tmplate">
            <input v-model="msg"/>
            <button v-on:click="notify()">Dispatch Event 派发事件，事件沿着父链冒泡</button>
        </template>
        <div>
            <p>{{messages | json}}</p>
            <childevent></childevent>
        </div>
    </div>
</div>
</body>
</html>
<script>
    var myComponent = Vue.extend({
        template: '<div><h1>this is  customer component</h1></div>',
        replace: false //是否替换模板
    })
    Vue.component('my-component', myComponent);
    Vue.component('child', {
        props: ['msg'],
        template: '<span>{{msg}}</span>'
    })
    Vue.component('childevent', {
        template: '#child-tmplate',
        data: function () {
            return {
                msg: 'hello'
            }
        },
        methods: {
            notify: function () {
                if (this.msg.trim() != '') {
                    this.$dispatch('child-msg', this.msg);
                    this.msg = '';
                }
            }
        }
    })
    var vm = new Vue({
        el: '#app',
        data: {
            messages: []
        },
        events: {
            'child-msg': function (msg) {
                this.messages.push(msg);
            }
        }
    })
</script>